var products = new Array();
var map = new Array();
var ping = 0;

var readOnly = 1;

	function loadProducts(prod){
		products = prod;
	}
	
	function setReadOnly(){
		readOnly = 1;
	}
	
	function makeItNotEditable(){
		readOnly = 1;
		document.getElementById("rp-zaglawie-edit").style.display = 'none';
		document.getElementById("rp-kategoria-edit").style.display = 'none';
		document.getElementById("rp-ukazania-edit").style.display = 'none';
		document.getElementById("product-add").style.display = 'none';
		//document.getElementById("rp-produkti-edit").style.display = 'none';
		//document.getElementById("rp-produkti").style.display = 'block';
		document.getElementById("rp-zaglawie").style.display = 'block';
		document.getElementById("rp-kategoria").style.display = 'block';
		document.getElementById("rp-ukazania").style.display = 'block';
		document.getElementById("rp-produkti").style.height = '377px';
		document.getElementById("rp-produkti").style.padding = '10px 20px';
		document.getElementById("save").style.display = 'none';
		if (document.getElementById("edit") != null){
			document.getElementById("edit").style.left = '606px';
			document.getElementById("edit").value = 'редактиране';	
		}
		
	}
	
	function makeItEditable(){
		readOnly = 0;
		changeMerna();
		document.getElementById("rp-zaglawie").style.display = 'none';
		document.getElementById("rp-kategoria").style.display = 'none';
		document.getElementById("rp-ukazania").style.display = 'none';
		//document.getElementById("rp-produkti").style.display = 'none';
		//document.getElementById("rp-produkti-edit").style.display = 'block';
		document.getElementById("product-add").style.display = 'block';
		document.getElementById("rp-zaglawie-edit").style.display = 'block';
		document.getElementById("rp-kategoria-edit").style.display = 'block';
		document.getElementById("rp-ukazania-edit").style.display = 'block';
		document.getElementById("rp-produkti").style.height = '305px';
		document.getElementById("rp-produkti").style.padding = '0px 20px';
		document.getElementById("save").style.display = 'block';
		if (document.getElementById("edit") != null){
			document.getElementById("edit").style.left = '456px';
			document.getElementById("edit").value = 'отмени промените';
		}
	}
	
	function enableEdit(){
		if (readOnly == 0){
			makeItNotEditable();
		} else {
			makeItEditable();
		}
		listTable();
	}

	function setProducts(){
		document.saveform.productsjs.value = products;
	}
	
	function addProducts(){
		var pName = document.getElementById("combo").value;
		var pQuantity = document.getElementById("qId").value;
		var merna = document.getElementById("mcombo").value;
		var productLine = new Array(pName, pQuantity, merna);
		for (i=0; i < products.length; i++){
			if (products[i][0] == pName){
				document.getElementById("qId").value = '';
				return;
			}
		}
		products.push(productLine);
		document.getElementById("qId").value = '';
		listTable();
	}

	function removeProduct(i){
		products.splice(i, 1);
		listTable();
	}
	
	

	function listTable(){
		var result = '<table>';
		result = '<p>продукти:</p>';
		result+='<table>';
		
		for (i=0;i<products.length;i++){
			result+='<tr>';
			for (j=0;j<products[i].length;j++){
				result+='<td class="p'+j+'">';
				result+=products[i][j];
				result+='</td>';
			}
			if (readOnly == 0){
				result+='<td>';
				result+='<input class="table-submit" type="button" value="-" onclick="removeProduct('+i+');" name="Remove" id="'+i+'">';
				result+='</td>';
			}
			
			result+='</tr>';
		}
		result+='</table>';
		document.getElementById("rp-produkti").innerHTML = result;
	}
	
	function submitRecepta(){
		setProducts();
		document.saveform.submit();
	}
	
	function changeMerna(){
		if (readOnly == 0){
			generateComboMerniEdinici(document.getElementById("combo").options[document.getElementById("combo").selectedIndex].title);
		}
		
	}
	
	function generateComboMerniEdinici(defaultValue){
		var result = '<select name="mcombo" id="mcombo">';
		result += '<option selected value="'+defaultValue+'">'+defaultValue+'</option>';
		for (i=0;i<map.length;i++){
				if (map[i][1] == defaultValue) {
					result += '<option value="'+map[i][2]+'">'+map[i][2]+'</option>';
				}
		}
		result += '</select>';
		document.getElementById("mernaEdinica").innerHTML = result;
	}
	
	function setMap(map1){
		map = map1;
	}
	
	function showHideShopList(){
		if (ping == 0){
			ping = 1;
			agent.call('','getList','receptaRefresh');
		} else {
			ping = 0;
			document.location.reload(1);
		}	
	}
	
	function receptaRefresh(result){
		list = result;
		document.getElementById("rpid-right").innerHTML = getFullList();
		 $('.edit').editable('save.php',{
			 id   : 'elementid',
	         name : 'newvalue',
	         indicator : '<img src="img/indicator.gif">',
			 type      : 'textarea',
	         cancel    : 'Cancel',
	         submit    : 'OK',
	         tooltip   : 'Цък за да редактираш...'
		 });
	}